<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>众筹分销</title>
    <%@include file="../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/table.css">
</head>
<body>
<!--头部-->
<%@include file="../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <div class="layui-tab-item layui-show">
                <div class="layui-tab layui-tab-brief" lay-filter="type">
                    <ul class="layui-tab-title" id="zc_tab">
                        <li <c:if test="${isCopartner == 0}">class="layui-this"</c:if>>众筹项目</li>
                        <li <c:if test="${isCopartner == 1}">class="layui-this"</c:if>>分销项目</li>
                    </ul>
                    <div class="layui-tab-content">
                        <form class="layui-form" action="${ctx}/activity/zcActivity/listForDistribution.do" id="myForm" method="post">
                            <input type="hidden" name="pageNo" id="pageNo"/>
                            <div class="f-search-bar">
                                <div class="search-container">
                                    <ul class="search-form-content">
                                        <li class="form-item-inline"><label class="search-form-lable">项目名称</label>
                                            <div class="layui-input-inline">
                                                <input type="text" name="title" autocomplete="off" class="layui-input" value="${activity.title}" placeholder="请输入项目名称">
                                            </div>
                                        </li>
                                        <li class="form-item-inline"><label class="search-form-lable">活动状态</label>
                                            <div class="layui-input-inline">
                                                <select name="timeStatus">
                                                    <option value="">全部</option>
                                                    <option value="0" ${input.timeStatus == 0 ? 'selected' : ''}>进行中</option>
                                                    <option value="1" ${input.timeStatus == 1 ? 'selected' : ''}>已结束</option>
                                                </select>
                                            </div>
                                        </li>
                                        <li class="form-item-inline">
                                            <div class="sub-btns">
                                                <a class="layui-btn layui-btn-danger"
                                                   href="javascript:submitFunction('#myForm')">查询</a>
                                                <a class="layui-btn layui-btn-normal"
                                                   href="javascript:resetFunction('#myForm')">重置</a>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </form>
                        <div class="my-act-list-content">
                            <ul class="num">
                                <a class="layui-btn layui-btn-danger layui-btn-sm" href="javascript:batchCancelDistribution()">批量取消分销</a>
                            </ul>
                            <div class="cl">
                                <c:set var="nowDate" value="<%=new Date()%>"></c:set>
                                <table class="layui-table layui-form">
                                    <colgroup>
                                        <col width="50px">
                                        <col width="20%">
                                        <col width="10%">
                                        <col width="100px">
                                        <col width="150px">
                                        <col width="150px">
                                        <col width="150px">
                                        <col width="100px">
                                        <col width="100px">
                                        <col>
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th><input type="checkbox" id="chkAll" lay-skin="primary" lay-filter="chkAll"/></th>
                                        <th>项目名称</th>
                                        <th>发布者</th>
                                        <th>活动金额</th>
                                        <th>发布时间</th>
                                        <th>众筹结束时间</th>
                                        <th>可提现日期</th>
                                        <th>活动状态</th>
                                        <th>分销人数</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <c:forEach var="activity" items="${activityList}" varStatus="st">
                                        <tr>
                                            <td><input type="checkbox" name="id" value="${activity.id}" lay-skin="primary" class="goods-chk" ${activity.disCount > 0 ? 'disabled' : ''} /></td>
                                            <td class="ellipsis-1 tl" title=" ${activity.title}">${activity.title}</td>
                                            <td class="table-member"
                                                onclick="openDialogShow('用户名片','${ctx}/system/member/memberView.do?id=${activity.member}','400px','470px')">
                                                <div class="member-cell">
                                                    <div class="member-logo common-img">
                                                        <img src="<c:if test="${activity.authorLogo != null}">${activity.authorLogo}?imageMogr2/auto-orient/crop/126x126</c:if>" alt="" onerror="txz.errImg(this)" onload="txz.imgLoad(this)">
                                                    </div>
                                                    <div class="member-name ellipsis-1">
                                                        <a class="blue" title="${activity.authorName}" href="javascript:void(0);">${activity.authorName}</a>
                                                    </div>
                                                </div>
                                            </td>
                                            <td>￥${activity.price}</td>
                                            <td><fmt:formatDate value="${activity.createDate}" pattern="yyyy-MM-dd HH:mm"/></td>
                                            <td><fmt:formatDate value="${activity.endTime}" pattern="yyyy-MM-dd HH:mm"/></td>
                                            <td><fmt:formatDate value="${activity.withdrawDate}" pattern="yyyy-MM-dd"/></td>
                                            <td>
                                                <c:if test="${activity.endTime > nowDate}">
                                                    <span class="red">进行中</span>
                                                </c:if>
                                                <c:if test="${activity.endTime < nowDate}">
                                                    <span>已结束</span>
                                                </c:if>
                                            </td>
                                            <td>${activity.disCount}</td>
                                            <td class="tb-opts">
                                                <div class="comm-opts">
                                                    <a class="qr-btn" href="javascript:txz.openQrDialog({
                                                                           title:'扫码二维码可预览分享',
                                                                           detailList:[
                                                                               {
                                                                               title:'报名二维码',
                                                                               url:'${activity.detailQrCode }'
                                                                               }
                                                                           ]
                                                                           })">二维码</a>
                                                    <a href="javascript:showBrokerage('${activity.title}',
                                                        '${activity.id}', '${activity.directBrokerage}',
                                                        '${activity.indirectBrokerage}', '${activity.remarks}');">查看佣金
                                                    </a>
                                                    <%--<c:if test="${empty activity.withdrawDate}">--%>
                                                        <a href="javascript:setWithdrawDate('${activity.id}', '${activity.title}',
                                                            '<fmt:formatDate value="${activity.withdrawDate}" pattern="yyyy-MM-dd"/>');">设置提现日期</a>
                                                    <%--</c:if>--%>
                                                    <c:if test="${activity.disCount == 0}">
                                                        <a href="javascript:cancelDistribution('${activity.id}');" target="_self">取消分销</a>
                                                    </c:if>
                                                </div>
                                            </td>
                                        </tr>
                                    </c:forEach>
                                    </tbody>
                                </table>
                                <div id="page_content" class="page-container"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div><!--底部-->
        <%@include file="../include/footer.jsp" %>
    </section>
    <%--设置可分销弹窗--%>
    <div id="distributionDiv" style="padding: 0px 10px">
        <div class="layui-form" lay-filter="distribution">
            <form id="distributionForm" class="layui-form mt20">
                <div class="layui-form-item" style="padding-left: 10%;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width: 115px;">招募可提佣金</label>
                        <div class="layui-input-inline">
                            <input type="number" name="directBrokerage" autocomplete="off" class="layui-input" min="0" readonly>
                        </div>
                        <label class="layui-form-mid">每人</label>
                    </div>
                </div>

                <div class="layui-form-item" style="padding-left: 17%;">
                    <div class="layui-inline">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-inline" style="width: 200px;">
                            每招募1个众筹成功人员可获得的佣金
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" style="padding-left: 10%;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width: 115px;">下级招募可提佣金</label>
                        <div class="layui-input-inline">
                            <input type="number" name="indirectBrokerage" autocomplete="off" class="layui-input" min="0" readonly>
                        </div>
                        <label class="layui-form-mid">每人</label>
                    </div>
                </div>
                <div class="layui-form-item" style="padding-left: 17%;">
                    <div class="layui-inline">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-inline" style="width: 200px;">
                            每招募一个下级合伙人，下级合伙人每招募1个众筹成功人员，该合伙人可获得的佣金
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" style="padding-left: 17%;">
                    <div class="layui-inline">
                        <label class="layui-form-label">提现说明</label>
                        <div class="layui-input-inline" style="width: 200px;">
                            <textarea class="layui-textarea" name="remarks" readonly></textarea>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <%-- 设置提现日期 --%>
    <div id="withdrawDiv" style="padding: 0px 10px">
        <div class="layui-form" lay-filter="distribution">
            <form id="withdrawForm" class="layui-form mt20">
                <div class="layui-form-item" style="padding-left: 10%;">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width: 115px;">可提现日期</label>
                        <div class="layui-input-inline">
                            <input type="text" name="withdrawDate" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript" src="${ctx}/script/common/list.js"></script>
<script type="text/javascript" src="${ctx}/script/common/table_option.js"></script>
<script type="text/javascript">
    txz.initHeader({
        nav: [{
            name: '众筹分销',
            curr: true
        }],
        info: '<span class="f12">共<b>${page.totalCount}</b>条记录',
        btns: []
    });
    $(function () {
        //加载分页
        loadPageNew({
            elem: 'page_content',
            totalCount: '${page.totalCount}',
            limit: '${page.limit}',
            page: '${page.page}'
        });

        //加载日历
        layui.use(['laydate', 'form', 'element'], function () {
            var laydate = layui.laydate, form = layui.form, element = layui.element;

            element.on('tab(type)', function (data) {
                if (data.index == 0) {
                    location.href = "${ctx}/activity/activity/zcActivityList.do";
                }
                else if (data.index == 1) {
                    location.href = "${ctx}/activity/zcActivity/listForDistribution.do";
                }
            });

            form.on('checkbox(chkAll)', function (data) {
                var checked = data.elem.checked;
                if (checked) {
                    $(".goods-chk").each(function (index, ele) {
                        var disabled = $(ele).prop("disabled");
                        if (!disabled) {
                            $(ele).prop("checked", true);
                        }
                    });
                } else {
                    $(".goods-chk").each(function (index, ele) {
                        var disabled = $(ele).prop("disabled");
                        if (!disabled) {
                            $(ele).prop("checked", false);
                        }
                    });
                }
                form.render('checkbox');
            })

            var withdrawDate = {
                elem: 'input[name=withdrawDate]'
            };

            laydate.render(withdrawDate);
        });
    });

    /**
     * 查看合伙人
     * @param targetName 主题名称
     * @param eventId 主题id
     */
    function showBrokerage(targetName, eventId, directBrokerage, indirectBrokerage, remarks) {
        var setPayIndex = layer.open({
            type: 1,
            area: ['600px', '500px'],
            title: '查看“' + targetName + '”项目的佣金',
            content: $("#distributionDiv"),
            btn: ['关闭'],
            success: function () { // 弹出后回调
                $('#distributionDiv').find('[name=directBrokerage]').val(directBrokerage);
                $('#distributionDiv').find('[name=indirectBrokerage]').val(indirectBrokerage);
                $('#distributionDiv').find('[name=remarks]').val(remarks);
            },
            end: function () { // 销毁后回调
                $('#distributionDiv').find('[name=directBrokerage]').val('');
                $('#distributionDiv').find('[name=indirectBrokerage]').val('');
                $('#distributionDiv').find('[name=remarks]').val('');
            },
            yes: function () { // 确定按钮
                layer.close(setPayIndex);
            }
        });
    }

    /**
     * 取消分销
     * @param activityId 活动id
     */
    function cancelDistribution(activityId) {
        layer.confirm('确定要取消分销吗', {
            icon: 3,
            title: '系统提示'
        }, function (index) {
            $.post('${ctx}/activity/zcActivity/cancelDistribution.do', {ids : activityId}, function (res) {
                if (res.success) {
                    util.layerMsgSuccess('取消成功', function () {
                        window.location.reload();
                    })
                } else {
                    util.layerMsgError('取消失败');
                }
            })
        });
    }

    /**
     * 设置提现日期
     * @param activityId 活动id
     * @param activityName 活动名称
     */
    function setWithdrawDate(activityId, activityName, withdrawDate) {
        var setPayIndex = layer.open({
            type: 1,
            area: ['500px', '200px'],
            title: '设置“' + activityName + '”项目的可提现日期',
            content: $("#withdrawDiv"),
            btn: ['确定', '关闭'],
            success: function () { // 弹出后回调
                $('#withdrawDiv').find('[name=withdrawDate]').val(withdrawDate);
            },
            end: function () { // 销毁后回调
                $('#withdrawDiv').find('[name=withdrawDate]').val('');
            },
            yes: function () { // 确定按钮
                var withdrawDate = $('#withdrawDiv').find('[name=withdrawDate]').val();
                if (!util.isValid(withdrawDate)) {
                    util.layerMsgWarn('可提现日期不能为空');
                    return false;
                }
                $.post('${ctx}/activity/zcActivity/setWithdrawDate.do',
                    {activityId: activityId, withdrawTime: withdrawDate}, function (res) {
                        if (res.success) {
                            util.layerMsgSuccess('设置成功', function () {
                                window.location.reload();
                            })
                        } else {
                            util.layerMsgError('设置失败');
                        }
                    })
            },
            btn2: function () {
                layer.close(setPayIndex);
            }
        });
    }

    /**
     * 批量取消分销
     * @returns {boolean}
     */
    function batchCancelDistribution() {
        var goods = $(".goods-chk:checked");
        var length = goods.length;
        if (length == 0) {
            util.layerMsgError("请先选择项目");
            return false;
        }
        var _idArray = new Array();
        goods.each(function (index, ele) {
            _idArray.push($(ele).val());
        });
        var ids = _idArray.join(",");
        cancelDistribution(ids);
    }
</script>
</body>
</html>